package top.anhanx.alipay.controller;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import top.anhanx.alipay.domain.AliPayInfo;
import top.anhanx.alipay.service.PayService;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author FF
 */
@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private PayService payService;

    /**
     * 阿里支付
     * @param outTradeNo
     * @param subject
     * @param totalAmount
     * @param body
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/alipay")
    public String alipay(String outTradeNo, String subject, String totalAmount, String body) {
        AliPayInfo aliPayInfo = new AliPayInfo();
        aliPayInfo.setOut_trade_no(outTradeNo);
        aliPayInfo.setSubject(subject);
        aliPayInfo.setTotal_amount(totalAmount);
        aliPayInfo.setBody(body);
        return payService.aliPay(aliPayInfo);
    }

    @GetMapping("/alipay/success")
    public String success(HttpServletRequest request, Model model) {
        Map<String, String[]> value = request.getParameterMap();
        model.addAttribute("value", JSON.toJSONString(value));
        return "success";
    }

    @ResponseBody
    @RequestMapping("/alipay/notify")
    public void notify (HttpServletRequest request) {
        Map<String, String[]> value = request.getParameterMap();
        System.out.println(JSON.toJSONString(value));
    }

}
